package com.ys.controller.api.hr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TbAttendance;
import com.ys.entity.sys.YsCol;
import com.ys.service.hr.TbAttendanceService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/tbAttendance")
public class ApiInterTbAttendanceController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TbAttendance> commonApiService;
	@Autowired
	private TbAttendanceService tbAttendanceService;
	public static Long MENU_ID = 10241L;
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTbAttendanceController.class);
//	@RequiresPermissions("tpFbaPlanOrder:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TbAttendance cond
			){
		cond.setOrderby("A.createTime DESC");
		return commonApiService.searchLay(cond);
	}

	/**
	 * 新增
	 */
	@RequestMapping("/toAdd")
	@ResponseBody
	public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
	) {
		try {
			logger.info("考勤管理新增传参：" + params);
			//修改
			return tbAttendanceService.add(params);
		} catch (Exception e) {
			logger.error("考勤管理新增出错：", e.getMessage(), e);
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	/**
	 * 编辑
	 */
	@RequiresPermissions("tbAttendance:edit")
	@PostMapping("/toEdit")
	@ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tbAttendanceService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TbAttendance setSeachSql(TbAttendance cond){
		//排序
		cond.setOrderby("A.Id DESC");
		cond = commonApiService.dataAuth(MenuId.TB_Attendance, cond);
		return  cond;
	}
	
	/**
	 * 导出
	 */
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TbAttendance cond){
		try {
			cond.setPage(0).setSize(999999);
			cond = setSeachSql(cond);
			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(MenuId.TB_Attendance)
					.setFrontShow(1)
					.setOrderby("A.SortNo asc")
					.queryList();
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();

			//处理数据
			ExportExcelUtils<TbAttendance> util = new ExportExcelUtils<>(new TbAttendance());
			String toFilePath = util.exportExcel(listData, listField, "考勤管理");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			logger.error("导出出错：",e.getMessage(),e);
			return R.error("导出失败，请稍后重试！");
		}
	}



	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TbAttendance cond){
		cond.setStatus(1);
		return commonApiService.searchLay(cond);

	}
}
